﻿<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%

request.setCharacterEncoding("UTF-8");
//String name = new String(request.getParameter("uname").getBytes("ISO-8859-1"),"utf-8");
String name=request.getParameter("uname");
String password=request.getParameter("upassword");
String email=request.getParameter("email");

out.print(name+password+email);
//数据库用户名
String userName="root";
//密码
String userPasswd="";
//数据库名
String dbName="a0713225307";

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String u ="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd+"&useUnicode=true&characterEncoding=UTF-8";

 // response.setContentType("text/html;charset=gbk");
 // request.setCharacterEncoding("gbk");
  
java.sql.Connection con = java.sql.DriverManager.getConnection(u,"root","");
//java.sql.Statement statement=con.createStatement();

String sql1="select * from users where uName='"+name+"'";
Statement statement=con.createStatement();
ResultSet rs1=statement.executeQuery(sql1);

if(rs1.next())
{
	response.sendRedirect("register.jsp?name=1&email=0&flag=0");
}
else
{
String	sql2="select * from users where email='"+email+"'";
ResultSet rs2=statement.executeQuery(sql2);
	
	if(rs2.next())
	{
		response.sendRedirect("register.jsp?name=0&email=1&flag=0");
	}
	else
	{
		String sql3="insert into users(uName,uPassword,email)values('"+name+"','"+password+"','"+email+"')";
 
           if(statement.executeUpdate(sql3)==1)
            {
	        response.sendRedirect("../index.jsp");
             }else{
                 response.sendRedirect("register.jsp?name=0&email=0&flag=1");
                  }	
     }
	 
}
 if(statement!=null){
  statement.close();
  }
  if(con!=null){
  con.close();
  }
  }catch(Exception e){
   e.printStackTrace();
   System.out.println(e);
  } 

%>